package com.detection;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dahuatech.hutool.core.thread.ThreadUtil;
import com.detection.common.config.DetectionConfig;
import com.detection.common.utils.CacheFileUtils;
import com.detection.tcp.client.NettyTcpClient;
import com.detection.tcp.domain.TCPMessage;
import com.detection.tcp.utils.CameraDetectionStatusUtils;
import com.detection.tcp.utils.DockingUtils;
import com.detection.yolo.service.CameraDetectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

@SpringBootApplication
@EnableScheduling
@EnableCaching
public class DetectionApplication implements CommandLineRunner {

    @Autowired
    private CameraDetectionService cameraDetectionService;
    @Autowired
    private NettyTcpClient nettyTcpClient;

    public static void main(String[] args) {
        SpringApplication.run(DetectionApplication.class, args);
    }

    @Override
    public void run(String... args) throws InterruptedException, IOException {

        ThreadUtil.execAsync(new Runnable() {
            @Override
            public void run() {
                if (!DetectionConfig.isDeployOneMachineEnabled()) {
//                    nu.pattern.OpenCV.loadLocally();
                    loadOpenCVLibrary();
//                    cameraDetectionService.startDetection();
//                    cameraDetectionService.startScheduledReplay();
                    CameraDetectionStatusUtils.SoftFirstStartStatus.put("firstStartStatusWLW",false);
                    while (true) {
                        Boolean firstStartStatus = CameraDetectionStatusUtils.SoftFirstStartStatus.get("firstStartStatus");
                        if (Objects.nonNull(firstStartStatus) && firstStartStatus.booleanValue()) {
                            break;
                        }
                        Boolean firstStartStatusWLW = CameraDetectionStatusUtils.SoftFirstStartStatus.get("firstStartStatusWLW");
                        if (Objects.nonNull(firstStartStatusWLW) && !firstStartStatusWLW.booleanValue()) {
                            TCPMessage tcpMessage = TCPMessage.builder()
                                    .flag("firstStartStatusWLW")
                                    .data("1")
                                    .campCode(DetectionConfig.getCampCode())
                                    .build();
                            DockingUtils.postForTCP(tcpMessage);
                        }

                        TCPMessage tcpMessage = TCPMessage.builder()
                                .flag("firstStartStatus")
                                .campCode(DetectionConfig.getCampCode())
                                .data("1")
                                .build();
                        nettyTcpClient.sendMessage(tcpMessage);

                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        });

        //读取本地缓存文件
        CacheFileUtils.initCacheFile(DetectionConfig.getEhcacheFilePath());
    }

    private void loadOpenCVLibrary() {
        String OS = System.getProperty("os.name").toLowerCase();
        System.out.println("当前系统为：" + OS);
        if (OS.contains("win")) {
            System.load(DetectionConfig.getOpencvLib());
            System.load(DetectionConfig.getOpenh264());
//            System.load("D:\\lib\\openh264-2.1.1-win64.dll");
        } else {
            String openh264 = DetectionConfig.getOpenh264();
            System.out.println("openh264插件位置为：" + openh264);
            System.load("/usr/local/opencv/share/java/opencv4/libopencv_java470.so");
        }
    }
}
